using System;
using System.Data;
using System.Data.SqlClient;

using Sunyi.Project.WebPoint.Common;
using Sunyi.Project.WebPoint.DataAccess;

using Sunyi.Project.WebPoint.DataContract;

namespace Sunyi.Project.WebPoint.DataAccess
{
    public partial class UserDao : IDao<User>
    {
        public User Select(Guid id)
        {
            User user = null;

            SqlConnection connection = ConnectionProvider.CreateConnection();
            SqlCommand command = connection.CreateCommand();
            command.CommandType = CommandType.StoredProcedure;
            command.CommandText = "procPR_User_Select";
            command.Parameters.Add(new SqlParameter("@Id", id));

            try
            {
                connection.Open();

                using (SqlDataReader reader = command.ExecuteReader())
                {
                    while (reader.Read())
                    {
                        user = CreateEntityByReader(reader);
                    }
                }
            }
            finally
            {
                ConnectionProvider.CloseConnection(connection);
            }

            return user;
        }

        public void Insert(User user)
        {
            SqlConnection connection = ConnectionProvider.CreateConnection();
            SqlCommand command = connection.CreateCommand();
            command.CommandType = CommandType.StoredProcedure;
            command.CommandText = "procPR_User_Insert";
            command.Parameters.Add(new SqlParameter("@Id", user.Id));
            command.Parameters.Add(new SqlParameter("@UserId", user.UserId));
            command.Parameters.Add(new SqlParameter("@UserName", user.UserName));
            command.Parameters.Add(new SqlParameter("@Password", user.Password));
            command.Parameters.Add(new SqlParameter("@Mail", user.Mail));
            command.Parameters.Add(new SqlParameter("@Department", user.Department));
            command.Parameters.Add(new SqlParameter("@IsActive", user.IsActive));

            foreach (SqlParameter parameter in command.Parameters)
            {
                if (parameter.Value == null || parameter.Value.ToString() == Guid.Empty.ToString()) parameter.Value = DBNull.Value;
            }

            try
            {
                connection.Open();
                command.ExecuteNonQuery();
            }
            finally
            {
                ConnectionProvider.CloseConnection(connection);
            }
        }

        public void Update(User user)
        {
            if (user.UpdateTimestamp == DateTime.MinValue) user.UpdateTimestamp = DateTime.Now;

            SqlConnection connection = ConnectionProvider.CreateConnection();
            SqlCommand command = connection.CreateCommand();
            command.CommandType = CommandType.StoredProcedure;
            command.CommandText = "procPR_User_Update";
            command.Parameters.Add(new SqlParameter("@Id", user.Id));
            command.Parameters.Add(new SqlParameter("@UserId", user.UserId));
            command.Parameters.Add(new SqlParameter("@UserName", user.UserName));
            command.Parameters.Add(new SqlParameter("@Password", user.Password));
            command.Parameters.Add(new SqlParameter("@Mail", user.Mail));
            command.Parameters.Add(new SqlParameter("@Department", user.Department));
            command.Parameters.Add(new SqlParameter("@IsActive", user.IsActive));
            command.Parameters.Add(new SqlParameter("@UpdateTimestamp", DateTime.Now));

            foreach (SqlParameter parameter in command.Parameters)
            {
                if (parameter.Value == null || parameter.Value.ToString() == Guid.Empty.ToString()) parameter.Value = DBNull.Value;
            }

            try
            {
                connection.Open();
                command.ExecuteNonQuery();
            }
            finally
            {
                ConnectionProvider.CloseConnection(connection);
            }
        }

        public void Delete(Guid id)
        {
            SqlConnection connection = ConnectionProvider.CreateConnection();
            SqlCommand command = connection.CreateCommand();
            command.CommandType = CommandType.StoredProcedure;
            command.CommandText = "procPR_User_Delete";
            command.Parameters.Add(new SqlParameter("@Id", id));

            try
            {
                connection.Open();
                command.ExecuteNonQuery();
            }
            finally
            {
                ConnectionProvider.CloseConnection(connection);
            }
        }

        #region Associated Entity Role Related

        const string Role_ColumnName = "UserId";
        const string Role_TableName = "PR_User_Role";
        const string Role_ValueColumnName = "RoleId";

        public void FillRoles(User user)
        {
            DaoHelper.FillEntity<Guid>(user.Roles, user.Id, Role_ColumnName, Role_TableName, Role_ValueColumnName);
        }

        public void SaveRoles(User user)
        {
            TransactionHelper.ProcessTransaction
            (() =>
                {
                    DaoHelper.RemoveEntity(user.Id, Role_ColumnName, Role_TableName);
                    DaoHelper.InsertEntity<Guid>(user.Roles, user.Id, Role_ColumnName, Role_TableName, Role_ValueColumnName);
                }
            );
        }

        #endregion

        #region Private Method

        internal static User CreateEntityByReader(IDataReader reader)
        {
            User user = new User();
            if (reader["Id"] != DBNull.Value) user.Id = (Guid)reader["Id"];
            if (reader["UserId"] != DBNull.Value) user.UserId = reader["UserId"] as string;
            if (reader["UserName"] != DBNull.Value) user.UserName = reader["UserName"] as string;
            if (reader["Password"] != DBNull.Value) user.Password = reader["Password"] as string;
            if (reader["Mail"] != DBNull.Value) user.Mail = reader["Mail"] as string;
            if (reader["Department"] != DBNull.Value) user.Department = reader["Department"] as string;
            if (reader["IsActive"] != DBNull.Value) user.IsActive = (Boolean)reader["IsActive"];
            if (reader["InsertTimestamp"] != DBNull.Value) user.InsertTimestamp = (DateTime)reader["InsertTimestamp"];
            if (reader["UpdateTimestamp"] != DBNull.Value) user.UpdateTimestamp = (DateTime)reader["UpdateTimestamp"];

            return user;
        }

        #endregion
    }
}